package edu.ccut.saturn.studio.logic.model.drop.logic;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipFile;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.internal.core.JarEntryFile;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.drop.LogicDropHandler;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnViewNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnWorkflowNodeInLogic;

public class LogicWfDropHandler extends LogicDropHandler {

	public Shape getShape(String filePath ,IProject project) {
		File file = new File(filePath);
		Document document = getDocument(file);
//		Node root=document.getDocumentElement();
		
		NodeList list = document.getDocumentElement().getElementsByTagName("information");
		Node node = list.item(0);
		
		String strKey =SaturnW3CUtil.getChildNodeValue(node, "key");
		
		String strVersion = SaturnW3CUtil.getChildNodeValue(node,"version");
		
		String strName = SaturnW3CUtil.getChildNodeValue(node, "name");
		
		String strDescription = SaturnW3CUtil.getChildNodeValue(node, "description");
		
		NodeList list1 = document.getDocumentElement().getElementsByTagName("activity");
		Node startActivety = getStartActivety(list1);
		
		List<SaturnParameter> parameterList = new ArrayList<SaturnParameter>();
		
		if(startActivety!=null){
			List<Node> formDataList = this.getFormData(startActivety);
			for(Node n :formDataList){
				String formDataKey = SaturnW3CUtil.getNodeAttributeValue(n, "key");
				String formDataName = SaturnW3CUtil.getNodeAttributeValue(n, "name");
				String formDataID = SaturnW3CUtil.getNodeAttributeValue(n, "id");
				SaturnParameter parameter = new SaturnParameter();
				parameter.setDescription(formDataName);
				parameter.setId(formDataID);
				parameter.setName(formDataKey);
				parameter.setValueKey(formDataKey);
				parameter .setClassType("java.lang.String");
				parameter.setPassingType("in");
				parameter.setScope("logic");
				parameterList.add(parameter);
			}
		}
		
		SaturnWorkflowNodeInLogic saturnWorkflowNodeInLogic = new SaturnWorkflowNodeInLogic();
		saturnWorkflowNodeInLogic.setName(strName);
		saturnWorkflowNodeInLogic.setVersion(strVersion);
		saturnWorkflowNodeInLogic.setDescription(strDescription);
		saturnWorkflowNodeInLogic.setKey(strKey);
		SaturnParameter saturnParameterADD = new SaturnParameter();
		saturnParameterADD.setId(String.valueOf(parameterList.size()+1));
		saturnParameterADD.setName("creator");
		saturnParameterADD.setClassType("java.lang.String");
		saturnParameterADD.setPassingType("in");
		saturnParameterADD.setScope("logic");
		parameterList.add(saturnParameterADD);
		saturnWorkflowNodeInLogic.setParameters(parameterList);
		
		return saturnWorkflowNodeInLogic;
	}
	
	private List<Node> getFormData(Node node){
		List<Node> formDataList = new ArrayList<Node>();
		NodeList list = node.getChildNodes();
		for(int i = 0 ;i<list.getLength();i++){
			if(list.item(i).getNodeName().equals("formData")){
				NodeList dataList = list.item(i).getChildNodes();
				for(int j = 0; j<dataList.getLength();j++){
					if("data".equals(dataList.item(j).getNodeName())){
						formDataList.add(dataList.item(j));
					}
				}
			}
		}
		System.out.println(formDataList.size());
		return formDataList;
	}
	
	private Node getStartActivety(NodeList nodelist){
		for(int i = 0;i<nodelist.getLength();i++){
			if("start".equals(SaturnW3CUtil.getNodeAttributeValue(nodelist.item(i), "type"))){
				return nodelist.item(i);
			}
		}
		return null;
	}

	@SuppressWarnings("restriction")
	public Shape getShape(JarEntryFile jarEntryFile) throws Exception {
		String bundleName = super.getBundleNameFromJar(jarEntryFile);
		jarEntryFile.getFullPath();
		String pathName = jarEntryFile.getFullPath().toOSString();
		int i = pathName.lastIndexOf("\\");
		String fileName = pathName.substring(i+1, pathName.length());
		JarPackageFragmentRoot jar = (JarPackageFragmentRoot) jarEntryFile
				.getPackageFragmentRoot();
		ZipFile zipFile = null;
		try {
			zipFile = jar.getJar();
		} catch (CoreException e) {
			e.printStackTrace();
		}
		zipFile.getName();
		
		SaturnViewNode viewerNode = new SaturnViewNode();
		viewerNode.setName(fileName);
		viewerNode.setType("jsp");
		viewerNode.setActionlocation("/"+bundleName + jarEntryFile.getFullPath());

		return viewerNode;
	}

	/**
	 * 输入XXX.Jsp，返回XXX。
	 * 
	 * @param name
	 */
	private String getComponentName(String name) {
		int i = name.lastIndexOf(".");
		if (i > 0) {
			return name.substring(0, i).trim();
		}
		return "";
	}
	private Document getDocument(File aFile) {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = dbf.newDocumentBuilder();
			Document doc = builder.parse(aFile);
			return doc;
		} catch (Exception e) {
		}
		return null;
	}

	@Override
	protected Shape getShape(Element root, String symbolicName) {
		return null;
	}
}
